/* @(#)GSOrderDetailModifyConfirmAction.java   
 *
 * Project: GS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Jenny Li    2006-2-27     新增
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.action;

import java.util.ArrayList;
import java.util.HashMap;

import system.LoginContext;

import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.bean.GSOrderDetailBean;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.business.GSOrderDeptInfoProcessor;
import com.hitrust.gs.business.GSOrderProcesser;
import com.hitrust.gs.business.GSUniteGwProcessor;
import com.hitrust.gs.dao.GSOrderDetailDAO;
import com.hitrust.gs.form.GSOrderReceiveSaveForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.vo.GSOrderDetailModifyVO;
import com.hitrust.gs.vo.GSOrderReceiveQueryVO;

/**
 * 巴士物资级订单明细调整保存
 */
public class GSOrderReceiveSaveAction extends GSBaseAction {

    public String execute(GSBaseForm aForm) throws Exception {
        // 为了在保存调整信息后执行查询，所以调用GSOrderDetailModifyQueryForm
        GSOrderReceiveSaveForm tForm = (GSOrderReceiveSaveForm) aForm;
        LoginContext operatorBean = (LoginContext) aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
        ArrayList tList = new ArrayList();
        ArrayList tNewBatchList = new ArrayList();
        // 是否采用分批签收业务
        boolean isBatch = false;
        boolean tCreatNewBatch = false;
        if (tForm.orderDetailIds2 != null && tForm.orderDetailIds2.length > 0) {
            for (int i = 0; i < tForm.orderDetailIds2.length; i++) {
                GSOrderReceiveQueryVO tOrderDetlModVO = new GSOrderReceiveQueryVO();
                tOrderDetlModVO.cursigninCount = tForm.cursigninCount2[i];
                tOrderDetlModVO.signinCount = tForm.cursigninCount2[i];
                tOrderDetlModVO.signin_person = tForm.signin_person[i];
                tOrderDetlModVO.signin_explain = tForm.signin_explain[i];
                tOrderDetlModVO.orderDetailId = tForm.orderDetailIds2[i];
                String tDate = "";
                // 信息调整
                if (tForm.cursigninDate2[i] != null && tForm.cursigninDate2[i].length() == 10) {
                    tDate = tForm.cursigninDate2[i].substring(0, 4) + "-" + tForm.cursigninDate2[i].substring(5, 7) + "-"
                            + tForm.cursigninDate2[i].substring(8);
                }

                tOrderDetlModVO.signin_date = tDate;
                tOrderDetlModVO.cursigninDate = tDate;

                tList.add(tOrderDetlModVO);

                // 如果签收数量未满额则创建分批列表
                if (isBatch) {
                    if (tOrderDetlModVO.cursigninCount < Double.parseDouble(tForm.getN_GoodsCount()[i])) {
                        GSOrderDetailModifyVO tGSOrderDetailModifyVO = new GSOrderDetailModifyVO();
                        // 取得新的批次号
                        GSOrderDetailDAO tGSOrderDetailDAO = new GSOrderDetailDAO();
                        ArrayList tBatchList = tGSOrderDetailDAO.getRelationBatchOrder(tForm.orderDetailIds2[i]);
                        String tNewOrderDetailId = ((GSOrderDetailBean) tBatchList.get(0)).getGs_order_detail_id();

                        int tPos = tNewOrderDetailId.indexOf("F_");
                        if (tPos == -1)
                            tNewOrderDetailId = tNewOrderDetailId + "F_1";
                        else {
                            tNewOrderDetailId = tNewOrderDetailId.substring(0, tPos + 2) + (Integer.parseInt(tNewOrderDetailId.substring(tPos + 2)) + 1);
                        }
                        // 取得所有批次总和后剩余的数量//待确认逻辑
                        // double tAllCount =0.0;
                        // for(int k=0;k<tBatchList.size();k++){
                        // tAllCount +=
                        // ((GSOrderDetailBean)tBatchList.get(k)).getGs_order_count();
                        // }
                        tGSOrderDetailModifyVO.orderDetailId = tNewOrderDetailId;
                        tGSOrderDetailModifyVO.concentricityId = tForm.getN_ConcentricityId()[i];
                        tGSOrderDetailModifyVO.manufName = tForm.getN_GoodsName()[i];
                        tGSOrderDetailModifyVO.providerName = tForm.getN_SupplyerName()[i];
                        tGSOrderDetailModifyVO.manufSpec = tForm.getN_GoodsSpec()[i];
                        tGSOrderDetailModifyVO.manufType = tForm.getN_GoodsModel()[i];
                        tGSOrderDetailModifyVO.manufBrand = tForm.getN_GoodsBrand()[i];
                        tGSOrderDetailModifyVO.orderCount = Double.parseDouble(tForm.getN_GoodsCount()[i]) - tOrderDetlModVO.cursigninCount;
                        tGSOrderDetailModifyVO.quantUnit = tForm.getN_GoodsUnit()[i];
                        tNewBatchList.add(tGSOrderDetailModifyVO);
                        tCreatNewBatch = true;
                    }
                }
            }
            try {
                GSOrderProcesser tOrderProc = new GSOrderProcesser();

                String msg = tOrderProc.modifyOrder_r(operatorBean, tList);

                aForm.getRequest().setAttribute("ISQUERYCONDITIONEXIST", "T");
                tForm.getRequest().setAttribute("message", msg);
                
                GSManufactureProcessor tGSManufProcessor = new GSManufactureProcessor();
                tGSManufProcessor.getManufactureOfLevel(aForm);
                GSUniteGwProcessor processer = new GSUniteGwProcessor();
                ArrayList ary = processer.getWareHouseLevelName(operatorBean.getLoginWarehouseCode(),operatorBean.getLoginWarehouseCode());
                aForm.getRequest().setAttribute("orderdept",ary);

                if (tCreatNewBatch)
                    return GSBaseConstants.FORWARD_CREAT_NEW_BATCH_ORDER;
                else
                    return GSBaseConstants.FORWARD_SUCCESS;

            } catch (Exception e) {
                tForm.getRequest().setAttribute("action", "A101301.do");
                throw e;
            }
        } else {
            tForm.getRequest().setAttribute("action", "A101301.do");
            throw new GSException(GSException.E00003, GSException.E00003_MSG);
        }
    }

}
